<script setup>
  import { ref, computed } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import taskApi from '@/apis/task.js'
  // 定义两个响应式变量用来接收uni-file-picker上传好的图片数据
  // 商品照片
  const deliverPictureList = ref([])
  // 凭证照片
  const certificatePictureList = ref([])
  // 获取url传入的参数id
  const taskId = ref(0)
  onLoad((params) => {
    taskId.value = params.id
  })

  // 控制按钮是否可以点击的
  const isEnable = computed(() => {
    return (
      deliverPictureList.value.length > 0 &&
      certificatePictureList.value.length > 0
    )
  })

  // 将表单数据提交到服务器
  async function onSubmit() {
    // 准备接口需要的请求数据
    const formData = {
      id: taskId.value,
      deliverPictureList: deliverPictureList.value,
      certificatePictureList: certificatePictureList.value,
    }

    try {
      // 调用接口
      let { code, msg } = await taskApi.delivery(formData)
      if (code !== 200) {
        return uni.utils.toast(msg)
      }

      // 跳转到task首页
      uni.switchTab({
        url: '/pages/task/index',
      })
    } catch (err) {
      // 真正开发的时候，服务器会提供一个log接口地址，我们将客户端发生的异常数据通过
      // 这个接口提交到服务器保存
      uni.utils.toast('您的网络异常，请稍后重试')
    }
  }
</script>

<template>
  <view class="page-container">
    <view class="receipt-info">
      <uni-file-picker
        v-model="certificatePictureList"
        limit="3"
        title="请拍照上传回单凭证"
      ></uni-file-picker>
      <uni-file-picker
        v-model="deliverPictureList"
        limit="3"
        title="请拍照上传货品照片"
      ></uni-file-picker>
    </view>
    <button :disabled="!isEnable" class="button" @click="onSubmit">提交</button>
  </view>
</template>

<style lang="scss" scoped>
  .page-container {
    padding: 30rpx;
  }

  .receipt-info {
    min-height: 600rpx;
    background-color: #fff;
    padding: 20rpx 30rpx;
    border-radius: 16rpx;

    ::v-deep .uni-file-picker {
      margin-bottom: 30rpx;
    }
  }

  .button {
    height: 100rpx;
    text-align: center;
    line-height: 100rpx;
    /* #ifdef APP */
    padding-top: 4rpx;
    /* #endif */
    border-radius: 100rpx;
    margin-top: 60rpx;
    color: #fff;
    font-size: $uni-font-size-big;
    background-color: $uni-primary;

    &[disabled] {
      color: #fff;
      background-color: #fadcd9;
    }
  }
</style>
